#ifndef DATA_REFLOW_DATA_BUFFER_H
#define DATA_REFLOW_DATA_BUFFER_H

#include "rclcpp/rclcpp.hpp"
#include "data_reflow_interfaces/msg/sensor_data.hpp"
#include "data_reflow/config_manager.h"
#include "data_reflow/sensor_type.h"
#include <unordered_map>
#include <deque>
#include <vector>
#include <mutex>
#include <memory>

namespace data_reflow
{
    class DataBuffer
    {
    public:
        // 单例获取接口（线程安全）
        static DataBuffer& get_instance();
        ~DataBuffer() = default;  // 公开析构，允许 unique_ptr 释放

        // 核心功能接口
        void addData(const data_reflow_interfaces::msg::SensorData& buffer_data);
        std::vector<data_reflow_interfaces::msg::SensorData> queryByTypeAndTime(
            SensorType type, const rclcpp::Time& start_time, const rclcpp::Time& end_time);
        std::vector<data_reflow_interfaces::msg::SensorData> getAllData(SensorType type);
        void clearByType(SensorType type);
        void clearAll();

    private:
        // 私有化构造（禁止外部新建）
        DataBuffer();
        DataBuffer(const DataBuffer&) = delete;
        DataBuffer& operator=(const DataBuffer&) = delete;

        // 工具函数
        bool isValidSensorType(SensorType type) const;
        size_t loadBufferSizeFromConfig() const;

        // 成员变量
        std::unordered_map<SensorType, std::deque<data_reflow_interfaces::msg::SensorData>> type_buffers_;
        size_t buffer_size_;  // 单类型最大缓存容量
        std::mutex buffer_mutex_;  // 线程安全锁
        static std::unique_ptr<DataBuffer> instance_;  // 静态单例指针（仅声明）
    };
}  // namespace data_reflow

#endif  // DATA_REFLOW_DATA_BUFFER_H